<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<title>修改部门</title>
	<link rel="icon" href="${pageContext.request.contextPath}/lib/static/login/images/favicon.ico">
	<link rel="stylesheet" href="${pageContext.request.contextPath}/lib/layui/css/layui.css">
	<script src="${pageContext.request.contextPath}/lib/layui/layui.js" charset="utf-8"></script>
</head>
<body>
<div class="layui-fluid">
	<hr class="layui-bg-red">
	<div class="layui-row">
		<div class="layui-col-sm6  layui-col-sm-offset3">
			<form class="layui-form layui-form-pane" id="departmentForm">
				<div class="layui-form-item">
					<label class="layui-form-label">部门名称</label>
					<div class="layui-input-block">
						<input type="hidden" name="id">
						<input type="text" id="deptName" name="deptName" autofocus lay-verify="required|deptName" lay-verType="tips" placeholder="请输入部门名称" class="layui-input">
					</div>
				</div>
                <%--<div class="layui-form-item">
                    <label class="layui-form-label">上级部门</label>
                    <div class="layui-input-inline">
                        <input type="hidden" id="superId">
                        <select name="superDeptId" id="selectSuperDept" lay-search>
                            <option value="">直接选择或搜索选择</option>
                        </select>
                    </div>
                </div>--%>
				<div class="layui-form-item">
					<label class="layui-form-label">部门详情</label>
					<div class="layui-input-block">
						<textarea id="deptDesc" name="deptDesc" lay-verify="required" lay-verType="tips" placeholder="请输入内容" class="layui-textarea"></textarea>
					</div>
				</div>
				<div class="layui-form-item">
					<div class="layui-input-block">
						<button class="layui-btn" lay-submit="" lay-filter="saveDept">立即提交</button>
						<button type="reset" class="layui-btn layui-btn-primary" onclick="window.location.href='${pageContext.request.contextPath}/sys/department/department_edit'">重置</button>
					</div>
				</div>
			</form>
		</div>
	</div>
</div>
<script>
    var baseUrl = '${pageContext.request.contextPath}/sys/department/';
    /*发送ajax请求访问后台*/
    function sendAjaxPostReq($, param, method) {
        $.post(
            baseUrl + method,
            param,
            function (result) {
                if (result.code == 0) {
                    layer.msg(result.msg, {icon: 1, time: 500}, function () {
                        parent.window.location.reload();
                    });
                } else {
                    layer.msg(result.msg, {icon: 5, anim: 6});
                }
            },
            "json"
        );
    }
    layui.use(['form', 'jquery'], function () {
        var form = layui.form;
        var $ = layui.$;
        /*上级部门下拉列表刷新options*/
        $(function () {
            $.ajax({
                type: "get",
                data:{"id":$("input[name='id']").val()},
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                url: "getAll",
                success: function (result) {
                    $.each(result.data, function (index, item) {
                        $("#selectSuperDept").append("<option value='" + item.id + "'>" + item.deptName + "</option>");
                    })
                    $("#selectSuperDept").val($("#superId").val());
                    form.render('select');
                }
            })
        })
        //自定义验证规则
        form.verify({
            deptName: function (value, item) { //value：表单的值、item：表单的DOM对象
                if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                    return '部门名称不能有特殊字符';
                }
                if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                    return '部门名称首尾不能出现下划线\'_\'';
                }
                if (/^\d+\d+\d$/.test(value)) {
                    return '部门名称不能全为数字';
                }
            },
        });
        /*表单提交*/
        form.on('submit(saveDept)', function (data) {
            sendAjaxPostReq($, data.field, "update");
            return false;
            l
        });
    });
</script>
</body>
</html>